package com.spring.sunflower.main.fragment;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.spring.sunflower.App;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseFragment;
import com.spring.sunflower.bean.ChatPeopleBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.conversation.CallRecordActivity;
import com.spring.sunflower.conversation.ChatActivity;
import com.spring.sunflower.conversation.ConversationManager;
import com.spring.sunflower.conversation.LatestVisitorActivity;
import com.spring.sunflower.conversation.MessageAdapter;
import com.spring.sunflower.conversation.MessagePresenter;
import com.spring.sunflower.conversation.StrangeActivity;
import com.spring.sunflower.conversation.SystemNoticeActivity;
import com.spring.sunflower.event.LastChatEvent;
import com.spring.sunflower.event.NewMessageComeEvent;
import com.spring.sunflower.event.PinnedConversationEvent;
import com.spring.sunflower.event.StrangeConversationChangedEvent;
import com.spring.sunflower.main.MainContract;
import com.spring.sunflower.util.AppUtils;
import com.spring.sunflower.util.CallRingtoneUtils;
import com.spring.sunflower.util.DateTimeUtils;
import com.spring.sunflower.util.SPUtils;
import com.spring.sunflower.util.StatusBarUtil;
import com.spring.sunflower.widget.MsgItemView;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMConversationListener;
import com.tencent.imsdk.v2.V2TIMConversationResult;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Kevin on 2021/5/28<br/>
 * <p>
 * Describe:<br/>
 */
public class MessageFragment extends BaseFragment<MessagePresenter> implements MainContract.MessageView, OnRefreshListener, View.OnClickListener {
    private List<V2TIMConversation> mConversationList = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;
    private RecyclerView mRecyclerView;
    private MessageAdapter mAdapter;
    private TextView tvTitle;
    private LinearLayout llFavComment, llCallRecord, llLatestVisitor;
    private LinearLayoutManager layoutManager;
    private LinkedList<V2TIMConversation> mTopConversationList = new LinkedList<>();
    private LinkedList<String> mTopUserIDData = new LinkedList<>();
    private MsgItemView mivSystem;
    private MsgItemView mivStrangeMsg;
    private MsgItemView mivOfficialService;
    private Toolbar toolbar;
    private List<ChatPeopleBean.DatasBean> mChatData = new ArrayList<>();
    private boolean isFirst = true;
    private boolean isFirstLoadData = true;

    @Override
    public boolean shouldRegisterEventBus() {
        return true;
    }

    @Override
    public int setLayoutResId() {
        return R.layout.fragment_message;
    }

    @Override
    public void initView() {
        toolbar = mView.findViewById(R.id.toolBar);
        StatusBarUtil.setPaddingSmart(mActivity, toolbar);
        tvTitle = mView.findViewById(R.id.tvTitle);
        tvTitle.setText(getString(R.string.title_message));
        refreshLayout = mView.findViewById(R.id.refreshLayout);
        mRecyclerView = mView.findViewById(R.id.rvRecyclerView);
        llFavComment = mView.findViewById(R.id.llFavComment);
        llCallRecord = mView.findViewById(R.id.llCallRecord);
        llLatestVisitor = mView.findViewById(R.id.llLatestVisitor);
        layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new MessageAdapter(R.layout.adapter_message_fragment, mConversationList);
        mAdapter.addHeaderView(getHeaderView());
        mRecyclerView.setAdapter(mAdapter);
//        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setEnableLoadMore(false);
        llFavComment.setOnClickListener(this);
        llCallRecord.setOnClickListener(this);
        llLatestVisitor.setOnClickListener(this);
        V2TIMManager.getConversationManager().setConversationListener(new V2TIMConversationListener() {
            @Override
            public void onSyncServerStart() {
                super.onSyncServerStart();
            }

            @Override
            public void onSyncServerFinish() {
                super.onSyncServerFinish();
            }

            @Override
            public void onSyncServerFailed() {
                super.onSyncServerFailed();
            }

            @Override
            public void onNewConversation(List<V2TIMConversation> conversationList) {
                super.onNewConversation(conversationList);
                for (int i = 0; i < conversationList.size(); i++) {
                    V2TIMConversation v2TIMConversation = conversationList.get(i);
                    if (showOfficialMsg(v2TIMConversation)) break;
                    if (showOfficialServiceMsg(v2TIMConversation)) break;
                    if (ConversationManager.isStrangeMsg(mChatData, conversationList.get(i).getUserID())) {
                        mStrangeListData.add(0, conversationList.get(i));
                        refreshStrangeItem(mStrangeListData.size());
                    } else {
                        mAdapter.addData(mTopConversationList.size(), conversationList);
                    }
                }
                printD("onNewConversation");
            }

            @Override
            public void onConversationChanged(List<V2TIMConversation> conversationList) {
                super.onConversationChanged(conversationList);
                printD("onConversationChanged=====" + conversationList.size());
                for (int i = 0; i < conversationList.size(); i++) {
                    V2TIMConversation v2TIMConversation = conversationList.get(i);
                    if (showOfficialMsg(v2TIMConversation)) break;
                    if (showOfficialServiceMsg(v2TIMConversation)) break;
                    printD("onConversationChanged  pinned=" + v2TIMConversation.isPinned());
                    boolean isStrange = ConversationManager.isStrangeMsg(mChatData, v2TIMConversation.getUserID());
//                    showToast("changed===" + isStrange);
                    if (isStrange) {//如果是陌生人而且在陌生人列表，而且不是置顶的就不做操作
                        V2TIMConversation conversation = ConversationManager.getConversation(mStrangeListData, v2TIMConversation.getUserID());
                        if (conversation != null) {//不为null说明在陌生人里面
                            int index = mStrangeListData.indexOf(conversation);
                            if (index != -1) {
                                mStrangeListData.set(index, v2TIMConversation);//替换成新的会话,通过eventBus告知陌生人页面
                                EventBus.getDefault().post(new StrangeConversationChangedEvent(v2TIMConversation.getUserID()));
                                refreshStrangeItem(mStrangeListData.size());
                            }
                        } else {//为null 数据已经改变
                            StrangeConversationChangedEvent event = new StrangeConversationChangedEvent();
                            event.setConversationIsNull(true);
                            EventBus.getDefault().post(event);
                            printD("conversation is null");
                        }
                    } else {
                        for (int j = 0; j < mConversationList.size(); j++) {
                            V2TIMConversation timConversation = mConversationList.get(j);
                            if (timConversation.getConversationID().equals(v2TIMConversation.getConversationID())) {
                                mAdapter.setData(j, v2TIMConversation);
                                break;
                            }
                        }
                    }
                }
                EventBus.getDefault().post(new NewMessageComeEvent());
            }
        });
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                V2TIMConversation v2TIMConversation = mConversationList.get(position);
                String conversationID = v2TIMConversation.getConversationID();
                V2TIMMessage lastMessage = v2TIMConversation.getLastMessage();
//                long lastMessageTimestamp = lastMessage.getTimestamp();
                String userID = v2TIMConversation.getUserID();
                String showName = v2TIMConversation.getShowName();
                String draftText = v2TIMConversation.getDraftText();
                String faceUrl = v2TIMConversation.getFaceUrl();
                printD("conversationID=" + conversationID);
                Intent intent = new Intent(mActivity, ChatActivity.class);
                intent.putExtra(Constant.IS_FROM_HELP, false);
                intent.putExtra(Constant.C2C_CONVERSATION_ID, conversationID);
                intent.putExtra(Constant.C2C_USER_ID, userID);
                intent.putExtra(Constant.C2C_SHOW_NAME, showName);
                intent.putExtra(Constant.C2C_DRAFT_TEXT, draftText);
                intent.putExtra(Constant.C2C_FACE_URL, faceUrl);
                startActivity(intent);
                saveField(Constant.KEY_USERID_CHATTER, userID);
                markMessageRead(userID);
            }
        });
    }

    public void markMessageRead(String userID) {
        V2TIMManager.getMessageManager().markC2CMessageAsRead(userID, new V2TIMCallback() {
            @Override
            public void onError(int code, String desc) {

            }

            @Override
            public void onSuccess() {

            }
        });
    }

    /**
     * 客服
     *
     * @param v2TIMConversation
     * @return
     */
    public boolean showOfficialServiceMsg(V2TIMConversation v2TIMConversation) {
        if (v2TIMConversation.getUserID().equals(Constant.OFFICIAL_USER_ID_SERVICE)) {
            int unreadCount = v2TIMConversation.getUnreadCount();
            V2TIMMessage lastMessage = v2TIMConversation.getLastMessage();
            refreshOfficialService(lastMessage, unreadCount);
            return true;
        }
        return false;
    }

    public boolean showOfficialMsg(V2TIMConversation v2TIMConversation) {
        if (v2TIMConversation.getUserID().equals(Constant.OFFICIAL_USER_ID)) {
            int unreadCount = v2TIMConversation.getUnreadCount();
            V2TIMMessage lastMessage = v2TIMConversation.getLastMessage();
            refreshOfficialNotice(lastMessage, unreadCount);
            return true;
        }
        return false;
    }

    private View getHeaderView() {
        View view = getLayoutInflater().inflate(R.layout.header_message_item, (ViewGroup) mRecyclerView.getParent(), false);
        mivSystem = view.findViewById(R.id.miv_system_notice);
        mivSystem.setLocalAvatarUrl(R.drawable.ic_system_notice);
//        mivSystem.setName("系统通知");
        mivSystem.setLastMsg("暂无系统消息");
        mivSystem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goSysNotice();
            }
        });
        mivStrangeMsg = view.findViewById(R.id.miv_strange_msg);
        mivStrangeMsg.setLocalAvatarUrl(R.drawable.ic_strange_message);
//        mivStrangeMsg.setName("陌生人消息");
        mivStrangeMsg.setLastMsg("暂无陌生人消息");
        mivStrangeMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goStrangeMsg();
            }
        });
        mivOfficialService = view.findViewById(R.id.miv_official_service);
        if (AppUtils.isVerify(getContext())) {//审核环境显示在线客服
            mivOfficialService.setVisibility(View.VISIBLE);
//            mivOfficialService.setRemoteAvatarUrl(Constant.OFFICIAL_SERVICE_AVATAR);
            mivOfficialService.setLocalAvatarUrl(R.drawable.sunflower_icon);
            mivOfficialService.setLastMsg("暂无新消息");
            mivOfficialService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CallRingtoneUtils.getInstance(getContext()).reset();
                    goOfficialService();
                }
            });
        } else {
            mivOfficialService.setVisibility(View.GONE);
            markMessageRead(Constant.OFFICIAL_USER_ID_SERVICE);
        }
        return view;
    }

    @Override
    public void initData() {
//        V2TIMManager.getConversationManager().getConversationList(0, 100, new V2TIMValueCallback<V2TIMConversationResult>() {
//            @Override
//            public void onError(int i, String s) {
//
//            }
//
//            @Override
//            public void onSuccess(V2TIMConversationResult v2TIMConversationResult) {
//                long nextSeq = v2TIMConversationResult.getNextSeq();
//                boolean finished = v2TIMConversationResult.isFinished();
//                List<V2TIMConversation> conversationList = v2TIMConversationResult.getConversationList();
////                conversationList.get(0).get
//                mAdapter.addData(conversationList);
//            }
//        });
    }

    @Override
    public void onResume() {
        super.onResume();
        printD("isUIVisible=" + isUIVisible + ",isViewCreated=" + isViewCreated);
        if (isFirst) {
            mPresenter.getChatPeople(true);
        } else {
            mPresenter.getChatPeople(false);
        }
    }

    public void getConversationListX() {
        V2TIMManager.getConversationManager().getConversationList(0, 100, new V2TIMValueCallback<V2TIMConversationResult>() {
            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onSuccess(V2TIMConversationResult v2TIMConversationResult) {
                long nextSeq = v2TIMConversationResult.getNextSeq();
                boolean finished = v2TIMConversationResult.isFinished();
                List<V2TIMConversation> conversationList = v2TIMConversationResult.getConversationList();
                List<V2TIMConversation> tempList = v2TIMConversationResult.getConversationList();
                mTopConversationList.clear();
                mTopUserIDData.clear();
                for (int i = 0; i < tempList.size(); i++) {
//                    printD("orderKey=" + tempList.get(i).getOrderKey());
                    if (tempList.get(i).isPinned()) {
                        mTopConversationList.add(tempList.get(i));
                        mTopUserIDData.add(tempList.get(i).getUserID());
                    }
                }
//                printD("mTopUserIDData.toString()=" + mTopUserIDData.toString());
                printD("tempList.size()=" + tempList.size());
                saveField(Constant.C2C_PINNED, mTopUserIDData.toString());
//                conversationList.addAll(0, mTopConversationList);
                List<V2TIMConversation> newConversationList = new ArrayList<>();
                List<V2TIMConversation> sysNoticeConversationList = new ArrayList<>();//系统通知
                List<V2TIMConversation> officialServiceConversationList = new ArrayList<>();//在线客服
                List<V2TIMConversation> newTempConversationList = new ArrayList<>();
                newTempConversationList.addAll(tempList)
                ;
//                if (mChatData.size() > 0) {//将聊过天的取出来显示
                int n = 0;
                for (int j = 0; j < tempList.size(); j++) {
                    V2TIMConversation conversation = tempList.get(j);
                    boolean strange = ConversationManager.isStrangeMsg(mChatData, conversation.getUserID());
                    if (conversation.getUserID().equals(Constant.OFFICIAL_USER_ID)) {
                        sysNoticeConversationList.add(conversation);//系统消息
                        int i = newTempConversationList.indexOf(conversation);
                        if (i != -1) {
                            newTempConversationList.remove(i);
                        }
                    }
                    if (conversation.getUserID().equals(Constant.OFFICIAL_USER_ID_SERVICE)) {
                        officialServiceConversationList.add(conversation);//系统消息
                        int i = newTempConversationList.indexOf(conversation);
                        if (i != -1) {
                            newTempConversationList.remove(i);
                        }
                    }

                    if (conversation.isPinned() | !strange) {
                        newConversationList.add(conversation);//聊过天的
                        int i = newTempConversationList.indexOf(conversation);
                        if (i != -1) {
                            newTempConversationList.remove(i);
                        }
                    }
                }
//                } else {//都是陌生人消息
//
//                }
                if (sysNoticeConversationList.size() > 0) {
                    V2TIMConversation conversation = sysNoticeConversationList.get(sysNoticeConversationList.size() - 1);
                    refreshOfficialNotice(conversation.getLastMessage(), conversation.getUnreadCount());
                }
                if (officialServiceConversationList.size() > 0) {
                    V2TIMConversation conversation = officialServiceConversationList.get(officialServiceConversationList.size() - 1);
                    refreshOfficialService(conversation.getLastMessage(), conversation.getUnreadCount());
                }
                int size = newTempConversationList.size();
                if (size > 0) {
                    mStrangeListData.clear();
                    mStrangeListData.addAll(newTempConversationList);
                    refreshStrangeItem(size);
                }
//                mAdapter.setList(conversationList);
                mAdapter.setList(newConversationList);
                if (refreshLayout.isRefreshing()) {
                    refreshLayout.finishRefresh();
                }
            }
        });
    }

    private void refreshOfficialService(V2TIMMessage lastMessage, int size) {
        long timestamp = lastMessage.getTimestamp();
        String timeFormatText = DateTimeUtils.getTimeFormatText(new Date(timestamp * 1000));
        mivOfficialService.setMsgTime(timeFormatText);
        String message = ConversationManager.getLastMessage(lastMessage);
        mivOfficialService.setLastMsg(message);
        if (size > 0) {
            mivOfficialService.setMsgCount(size + "");
            mivOfficialService.showMsgCount(true);
        } else {
            mivOfficialService.showMsgCount(false);
        }
        mAdapter.notifyDataSetChanged();
    }

    private void refreshOfficialNotice(V2TIMMessage lastMessage, int size) {
        long timestamp = lastMessage.getTimestamp();
        String timeFormatText = DateTimeUtils.getTimeFormatText(new Date(timestamp * 1000));
        mivSystem.setMsgTime(timeFormatText);
        String message = ConversationManager.getLastMessage(lastMessage);
        mivSystem.setLastMsg(message);
        if (size > 0) {
            mivSystem.setMsgCount(size + "");
            mivSystem.showMsgCount(true);
        } else {
            mivSystem.showMsgCount(false);
        }
        mAdapter.notifyDataSetChanged();
    }

    private void refreshStrangeItem(int size) {
        if (size > 0) {
            mivStrangeMsg.setLastMsg("已收到" + size + "个陌生人的消息");
            int i = 0;
            for (V2TIMConversation conversation : mStrangeListData) {
                printD("conversation.getUnreadCount()=" + conversation.getUnreadCount());
                i += conversation.getUnreadCount();
            }
//            showToast("i="+i+","+mStrangeListData.size());
            mivStrangeMsg.setMsgCount(i + "");
            if (i > 0) {
                mivStrangeMsg.showMsgCount(true);
            } else {
                mivStrangeMsg.showMsgCount(false);
            }
        } else {
            mivStrangeMsg.setLastMsg("暂无陌生人消息");
            mivStrangeMsg.showMsgCount(false);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void loadData() {
        if (isFirstLoadData) {
            mPresenter.getChatPeople(true);
            isFirstLoadData = false;
        } else {
            mPresenter.getChatPeople(false);
        }
    }

    @Override
    public MessagePresenter setPresenter() {
        return new MessagePresenter(this);
    }

//    @Override
//    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
//
//    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        mPresenter.getChatPeople(false);
        getConversationListX();
    }

    @Subscribe
    public void refreshConversationList(PinnedConversationEvent event) {
        String userID = event.getUserID();
        boolean pinned = event.isPinned();
        printD("v=======>" + "pinned=" + pinned);
        //如果是陌生人消息就添加进去
        if (ConversationManager.isStrangeMsg(mChatData, userID)) {
            V2TIMConversation conversation = ConversationManager.getConversation(mStrangeListData, userID);
            if (conversation != null) {
                printD("conversation" + conversation.isPinned());
                mConversationList.add(conversation);
                mStrangeListData.remove(conversation);
                refreshStrangeItem(mStrangeListData.size());
            } else {//是陌生人但是之前置顶了，现在取消了就要加到陌生人列表里面
                V2TIMConversation timConversation = ConversationManager.getConversation(mConversationList, userID);
                if (timConversation != null) {
                    mAdapter.remove(timConversation);
                    mStrangeListData.add(timConversation);
                    refreshStrangeItem(mStrangeListData.size());
                }
            }
        } else {

        }

        List<V2TIMConversation> tempList = mConversationList;
        for (int i = 0; i < mConversationList.size(); i++) {
            V2TIMConversation v2TIMConversation = mConversationList.get(i);
            String id = v2TIMConversation.getUserID();
            if (id.equals(userID)) {
                printD("v=======>" + v2TIMConversation.isPinned());
                tempList.remove(i);
                if (pinned) {//置顶了就添加
                    tempList.add(0, v2TIMConversation);
//                    tempList.add(mTopConversationList.size(), v2TIMConversation);
                    if (!mTopConversationList.contains(v2TIMConversation)) {
                        mTopConversationList.add(0, v2TIMConversation);
                    }
                    if (!mTopUserIDData.contains(userID)) {
                        mTopUserIDData.add(0, userID);
                    }
                } else {//取消置顶就从置顶集合中移除
                    if (mTopUserIDData.contains(userID)) {
                        mTopUserIDData.remove(userID);
                    }
                    if (mTopConversationList.contains(v2TIMConversation)) {
                        mTopConversationList.remove(v2TIMConversation);
                    }
                    printD("mTopConversationList.size=" + mTopConversationList.size());
                    if (mTopConversationList.size() > 0) {
                        tempList.add(mTopConversationList.size() - 1, v2TIMConversation);
                    } else {
                        tempList.add(mTopConversationList.size(), v2TIMConversation);

                    }
                }
                saveField(Constant.C2C_PINNED, mTopUserIDData.toString());
                printD("hello");
                mAdapter.setList(tempList);
                break;
            }
        }
    }

    @Subscribe
    public void refreshConversationList(LastChatEvent event) {
        String userID = event.getUserID();
        //如果是陌生人消息就添加进去,并从陌生人消息中移除
        if (ConversationManager.isStrangeMsg(mChatData, userID)) {
            V2TIMConversation conversation = ConversationManager.getConversation(mStrangeListData, userID);
            if (conversation != null) {
                mConversationList.add(conversation);
                mStrangeListData.remove(conversation);
                refreshStrangeItem(mStrangeListData.size());
            }
        }
        List<V2TIMConversation> tempList = mConversationList;
        markMessageRead(userID);
        for (int i = 0; i < mConversationList.size(); i++) {
            V2TIMConversation v2TIMConversation = mConversationList.get(i);
            String id = v2TIMConversation.getUserID();
            if (id.equals(userID) && i != 0) {
                tempList.remove(i);
                if (v2TIMConversation.isPinned()) {
                    tempList.add(0, v2TIMConversation);
                } else {
                    tempList.add(mTopConversationList.size(), v2TIMConversation);
                }
                mAdapter.setList(tempList);
                break;
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llFavComment:
                showToast("该功能待开放...");
                break;
            case R.id.llCallRecord:
                startNewActivity(CallRecordActivity.class);
                break;
            case R.id.llLatestVisitor:
                goLatestVisitor();
                break;
        }
    }

    private void goLatestVisitor() {
        Intent intent = new Intent(mActivity, LatestVisitorActivity.class);
        startActivity(intent);
    }

    private void goSysNotice() {
        Intent intent = new Intent(mActivity, SystemNoticeActivity.class);
        intent.putExtra(Constant.C2C_CONVERSATION_ID, Constant.OFFICIAL_C2C_CONVERSATION_ID);
        intent.putExtra(Constant.C2C_USER_ID, Constant.OFFICIAL_USER_ID);
        intent.putExtra(Constant.C2C_SHOW_NAME, "系统消息");
        intent.putExtra(Constant.C2C_DRAFT_TEXT, "");
        intent.putExtra(Constant.C2C_FACE_URL, "");
        startActivity(intent);
        markMessageRead(Constant.OFFICIAL_USER_ID);
    }

    private void goStrangeMsg() {
        Intent intent = new Intent(mActivity, StrangeActivity.class);
        startActivity(intent);
    }

    private void goOfficialService() {
        Intent intent = new Intent(mActivity, ChatActivity.class);
        intent.putExtra(Constant.IS_FROM_HELP, true);
        intent.putExtra(Constant.C2C_CONVERSATION_ID, Constant.OFFICIAL_SERVICE_C2C_CONVERSATION_ID);
        intent.putExtra(Constant.C2C_USER_ID, Constant.OFFICIAL_USER_ID_SERVICE);
        intent.putExtra(Constant.C2C_SHOW_NAME, getString(R.string.official_service));
        intent.putExtra(Constant.C2C_DRAFT_TEXT, "");
        intent.putExtra(Constant.C2C_FACE_URL, Constant.OFFICIAL_SERVICE_AVATAR);
        startActivity(intent);
        markMessageRead(Constant.OFFICIAL_USER_ID_SERVICE);
    }

    @Override
    public void showChatPeople(List<ChatPeopleBean.DatasBean> data, boolean first) {
        printD("showChatPeople");
        if (data != null) {
            mChatData.clear();
            mChatData.addAll(data);
        }
        Iterator<V2TIMConversation> iterator = mStrangeListData.iterator();
        while (iterator.hasNext()) {//查看陌生人结合中是否存在非陌生人，有的话就移除
            V2TIMConversation next = iterator.next();
            if (!ConversationManager.isStrangeMsg(mChatData, next.getUserID())) {
                iterator.remove();
                mAdapter.addData(0, next);
            }
        }
        refreshStrangeItem(mStrangeListData.size());
        if (first) {
            getConversationListX();
            isFirst = false;
        }

    }
}
